import socket
import msgpasser;
from msg import *;
import sys;
import os;
from CheckIn import check_in
from CheckOut import check_out
from QueryUsers import query_users
from GetDataFromAuthServer import test_cookie
import CacheMessage
import GetMessage
import nav

name = sys.argv[1]
print name
mp = msgpasser.messagePasser(name,'l')
msgpasser.mp = mp

srvSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srvSock.bind(('', 12345))
srvSock.listen(5)

while 1:
    cliSock, cliAddr = srvSock.accept()
    msg = cliSock.recv(1024)
    print "Message = %s" % msg
    t = msg.split(";");
    if(t[0] == "check_in"):
        if(len(t) < 5):
            cliSock.send("NACK")
            cliSock.close()
            continue
        uname = t[1]
        cookie = t[2]
        status = t[3]
        availability = t[4]
        if(test_cookie(uname, cookie) == "BAD_COOKIE"):
            cliSock.send("BAD_COOKIE")
        else:
            #check_in(uname, cookie, status, availability);
            res = check_in(mp, uname, cookie, status, availability)
            if(res == True):
                cliSock.send("ACK");
            else:
                cliSock.send("NACK");
    elif(t[0] == "check_out"):
        if(len(t) < 2):
            cliSock.send("NACK")
            cliSock.close()
            continue
        res = check_out(mp, t[1])
        if(res == True):
            cliSock.send("ACK")
        else:
            cliSock.send("NACK")
    elif(t[0] == "directions"):
        if(len(t) < 5):
            cliSock.send("NACK")
            cliSock.close()
            continue
        src = t[1]
        cookie = t[2]
        dst = t[3]
        uname = t[4]
        if(test_cookie(uname, cookie) == "BAD_COOKIE"):
            cliSock.send("BAD_COOKIE")
            cliSock.close()
            continue
        g = nav.Graph()
        cliSock.send(g.nav_str(src, dst))
    elif(t[0] == "query_users"):
        cookie = t[1]
        uname = t[2]
        if(test_cookie(uname, cookie) == "BAD_COOKIE"):
            cliSock.send("BAD_COOKIE")
            cliSock.close()
            continue
        ret = query_users()
        print ret
        cliSock.send(ret)
    elif(t[0] == "get_message"):
        uname = t[1]
        cookie = t[2]
        if(test_cookie(uname, cookie) == "BAD_COOKIE"):
            cliSock.send("BAD_COOKIE")
            cliSock.close()
            continue
        ret =GetMessage.get_messages_front_end(mp,t[1],mp.thisName)
        if ret==False:
            cliSock.send("no_messages")
        elif ret=="":
            cliSock.send("no_messages")
        else:
            cliSock.send(ret)
    elif(t[0] =="send_message"):
        uname = t[1]
        cookie = t[2]
        if(test_cookie(uname, cookie) == "BAD_COOKIE"):
            cliSock.send("BAD_COOKIE")
            cliSock.close()
            continue
        res=CacheMessage.cache_message(mp, t[1],t[3],t[4],t[2])
        if(res == True):
            cliSock.send("ACK")
        elif res==False:
            cliSock.send("NACK")
        else:
            cliSock.send("INVALID")
        #['send_message','asantell','cookie','hi','sending..','\n']

    print t
    #cliSock.send(msg)
    cliSock.close()
